<?php
namespace app\admin\controller;

class User extends Base{

    /**
     * 头像上传
     */
    public function userface($is_copy = false){
        if(request()->isPost()){
            if($is_copy){
                $userface = img_copy(input('imgUrl'), input('imgInitW'), input('imgInitH'), input('imgW'), input('imgH'), input('imgX1'), input('imgY1'), input('cropW'), input('cropH'), input('rotation'));
                $this->auth_user->detail->userface = $userface;
                $this->auth_user->detail->save();
                exit(json_encode([
                    "status" => 'success',
                    "url" => $userface . '?r=' . time()
                ]));
            }else{
                img_upload('userface', $this->auth_user->id, $_FILES['img']);
            }
        }
    }

    /**
     * 个人信息
     */
    public function profile(){
        if(request()->isPost()){
            if($x = input('nickname/s')) $this->auth_user->detail->nickname = $x;
            $this->auth_user->detail->gender = input('gender/d');
            if($x = input('birthday/s')) $this->auth_user->detail->birthday = $x;
            if($x = input('diploma/s')) $this->auth_user->detail->diploma = $x;
            if($x = input('school/s')) $this->auth_user->detail->school = $x;
            if($x = input('hometown/s')) $this->auth_user->detail->hometown = $x;
            if($x = input('address/s')) $this->auth_user->detail->address = $x;
            if($x = input('zipcode/s')) $this->auth_user->detail->zipcode = $x;
            if($x = input('signature/s')) $this->auth_user->detail->signature = $x;
            if($x = input('brief/s')) $this->auth_user->detail->brief = $x;
            if($this->auth_user->detail->save()) $this->result([], 1000, '保存成功', 'json');
            $this->result([], 1001, '保存失败', 'json');
        }
        return $this->fetch();
    }

    /**
     * 发送绑定验证码
     */
    public function send_code(){
        if(request()->isPost()){
            $type = input('type/s');
            $value = input('value/s');
            if($type == 'mobile'){}elseif($type == 'email'){}
            session("{$type}_code", 1111);
            $this->result([], 1000, '绑定成功', 'json');
        }
    }

    /**
     * 绑定信息
     */
    public function bind(){
        if(request()->isPost()){
            $code = input('code/s');
            $type = input('type/s');
            $value = input('value/s');
            if($code != session("{$type}_code")) $this->result([], 1001, '验证码错误', 'json');
            $this->auth_user->$type = $value;
            $this->auth_user->save();
            $this->result([], 1000, '绑定成功', 'json');
        }
    }

    /**
     * 角色
     */
    public function role($operate = ''){
        if(request()->isPost()){
            switch($operate){
                case 'refresh':
                    $list = \model\Role::order('sort', 'desc')->select();
                    $this->result($list, 1000, '刷新成功', 'json');
                    break;
                case 'sort':
                    $data = json_decode(input('data/s', ''), true) ?: [];
                    $data = array_values(array_reverse($data));
                    array_walk($data, function (&$v, $k){
                        $v = [
                            'id' => $v['id'],
                            'sort' => $k + 1
                        ];
                    });
                    (new \model\Role())->saveAll($data);
                    $this->result([], 1000, '排序成功', 'json');
                    break;
                case 'edit':
                    ;
                    break;
                case 'del':
                    \model\Role::destroy(input('id/d'));
                    $this->result([], 1000, '删除成功', 'json');
                    break;
                case 'add':
                    \model\Role::create([
                        'name' => input('name/s')
                    ]);
                    $this->result([], 1000, '添加成功', 'json');
                    break;
            }
        }
        $role_list = \model\Role::order('sort', 'desc')->select();
        $this->assign('role_list', $role_list);
        $permission_list = \model\Permission::bulid_tree();
        $this->assign('permission_list', $permission_list);
        return $this->fetch();
    }
}
